package com.relsell.utility;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.relsell.admin.DbHelper;

public class ChangeRoleService extends Service {

	AsyncTask<Void, Void, Void> mRegisterTask;
	String responseString;
	String username, usertype, userid;

	@Override
	public IBinder onBind(Intent intent) {
		Toast.makeText(getApplicationContext(), "Inside onBind",
				Toast.LENGTH_LONG).show();
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Toast.makeText(getApplicationContext(), "Isnide Oncrete",
				Toast.LENGTH_LONG).show();
		if(AppVars.isLogEnabled) {
		Log.w(AppVars.LOG_TAG, "inside  onCreate");
		}
		mRegisterTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				try {

					HttpClient http = new DefaultHttpClient();
					String server = AppVars.getServer();
					HttpPost post = new HttpPost("http://" + server
							+ "/gcminteraction/view/start.php?controlVar=18");

					HttpResponse response = http.execute(post);
					responseString = new BasicResponseHandler()
							.handleResponse(response);

					Log.w("Response String =", responseString);

					JSONObject obj;

					try {
						obj = new JSONObject(responseString);
						JSONArray data = obj.getJSONArray("data");

						for (int i = 0; i < data.length(); i++) {
							JSONObject c = data.getJSONObject(i);

							String userid = c.getString("userid");
							String username = c.getString("username");
							String usertype = c.getString("user_type");
							Log.w("userid", userid);
							Log.w("usernme", username);
							Log.w("usertype", usertype);

							DbHelper db = new DbHelper(ChangeRoleService.this);
							db.open();
                            Cursor cursor = db.readUserTableData();
                            String userId = null;
                            int count = cursor.getCount();
                            boolean updateNeeded = false;
                            while(count > 0){
                            	cursor.moveToNext();
                            	userId = cursor.getString(cursor
                    					.getColumnIndex(DbHelper.FIELD_USER_ID));
                            	if (userId.equals(userid)){
                            		db.updateUserTableData(userid, username, usertype);
                            		updateNeeded = true;
                            		break;
                            	}
                            	count --;
                            }
                            
                            if (updateNeeded == false) {
							    db.insertInUserTable(userid, username, usertype);
                            } 
							db.close();
						}

				
					} catch (JSONException e) {
						e.printStackTrace();
					}

				} catch (ClientProtocolException e) {

				} catch (IOException e) {

				}
				return null;

			}

			@Override
			protected void onPostExecute(Void result) {
				stopSelf();
			}
		};
		mRegisterTask.execute(null, null, null);

	}

	@Override
	public void onDestroy() {
		if (AppVars.isLogEnabled){
			Log.v(AppVars.LOG_TAG,"Service is stopped and Called");
		}
		super.onDestroy();
	}

}